
///<reference path='./Stars.ts'/>
///<reference path='./Ship.ts'/>
///<reference path='./GameStar.ts'/>
///<reference path='./UILayer.ts'/>
///<reference path='./utils/BulkLoader.ts'/>
///<reference path='./utils/ImageLoader.ts'/>
// import { Stars } from "./Stars";
// import { Ship } from "./Ship";
// import { GameStar } from './GameStar';
// import { UILayer } from './UILayer';
// import {BulkLoader} from './utils/BulkLoader';
// import {ImageLoader} from './utils/ImageLoader';
class App {
    public static loadManager: BulkLoader;
    private _stage: lg.Stage;
    private _resSuffix: string = "@2x";
    private _gameStar: GameStar;
    public static curScore: number;
    private _scoreText: lg.Text;
    public isDebug: boolean = true;
    public setScore(): void {
        GameStar.moveY += 30;
        App.curScore++;
        this.ship.blinkStop();
        if (App.curScore == 100) App.curScore = 0;
        this._gameStar.setScore(App.curScore);
        // console.log(App.curScore);
        this.changeScore(App.curScore)

    }
    public changeScore(v)
    {
        App.curScore=v;
        this._scoreText.text = v + "分";
    }
    constructor() {
        // alert("app")
        this._stage = new lg.Stage("canvasElement");

        App.loadManager = BulkLoader.getInstance();
        App.loadManager.addEventListener(LoaderEvent.LOAD_COMPLETE, this.handleComplete, this);


        //App.loadManager = new lg.LoadQueue(true);
        // App.loadManager.installPlugin(() => lg.Sound);

        // App.loadManager.on("complete", this.handleComplete, this);
        //this._loadManager.loadFile("bin/assets/images/2.jpg");


        var loadFiles = [
            { id: "main-body", src: "../res/assets/images/main/main-body" + this._resSuffix + ".png" },
            { id: "main-jupiter", src: "../res/assets/images/main/main-jupiter" + this._resSuffix + ".png" },
            { id: "main-mars", src: "../res/assets/images/main/main-mars" + this._resSuffix + ".png" },
            { id: "main-mercury", src: "../res/assets/images/main/main-mercury" + this._resSuffix + ".png" },
            { id: "main-moon", src: "../res/assets/images/main/main-moon" + this._resSuffix + ".png" },
            { id: "main-saturn", src: "../res/assets/images/main/main-saturn" + this._resSuffix + ".png" },
            { id: "main-sun", src: "../res/assets/images/main/main-sun" + this._resSuffix + ".png" },
            { id: "main-venus", src: "../res/assets/images/main/main-venus" + this._resSuffix + ".png" },
            { id: "earth", src: "../res/assets/images/earth" + this._resSuffix + ".png" },

            { id: "star", src: "../res/assets/images/star.png" },
            // { id: "astronaut", src: "bin/assets/images/astronaut" + this._resSuffix + ".png" },
            // { id: "celestial-body1", src: "bin/assets/images/celestial-body1" + this._resSuffix + ".png" },
            // { id: "celestial-body2", src: "bin/assets/images/celestial-body2" + this._resSuffix + ".png" },
            // { id: "meteorolite1", src: "bin/assets/images/meteorolite1" + this._resSuffix + ".png" },
            // { id: "meteorolite2", src: "bin/assets/images/meteorolite2" + this._resSuffix + ".png" },

            // { id: "shooting-star", src: "bin/assets/images/shooting-star" + this._resSuffix + ".png" },
            // { id: "satellite", src: "bin/assets/images/satellite" + this._resSuffix + ".png" },

            { id: "progress_bg", src: "../res/assets/images/progress_bg.png" },
            { id: "progress_bar", src: "../res/assets/images/progress_bar.png" },
            { id: "btnback", src: "../res/assets/images/btnback.png" },
            { id: "btnmusic", src: "../res/assets/images/btnmusic.png" }
        ];
        var ufosArr = window["ufosArr"];
        for (var i = 0; i < ufosArr.length; i++) {
            var ufo = ufosArr[i];
            loadFiles.push(
                { id: ufo.name, src: "../res/assets/images/" + ufo.name + this._resSuffix + ".png" }
            );
        }
        for (var j = 0; j < loadFiles.length; j++) {
            var o = loadFiles[j]
            App.loadManager.addFile(new ImageLoader(o.src), o.id);
            console.log(o.id)
        }
        App.loadManager.load();
        // App.loadManager.loadManifest(loadFiles);

        // App.loadManager.load();
        this._scoreText = new lg.Text("0分", "40px Arial");
        lg.Ticker.addEventListener("tick", this._stage);
    }
    private ship: Ship;
    private intervalGo() {
        App.curScore = 0;
        this.ship = new Ship();
        var starsLayer: Stars = new Stars(this.ship);
        this._stage.addChild(starsLayer);
        //this._stage.addEventListener("tick",(e)=>this.tickHandler(e));

        this._gameStar = new GameStar();
        this._stage.addChild(this._gameStar);
        // this._gameStar.setScore(0);
        //this._gameStar.y=500;
        //this._gameStar.setScore(1);

        this._stage.addChild(this.ship);
        this.ship.x = this._stage.getBounds().width - this.ship.getBounds().width >> 1;
        var canvas: HTMLCanvasElement = <HTMLCanvasElement>this._stage.canvas;
        this._scoreText.x = canvas.width >> 1;
        this._scoreText.textAlign = "center";
        //console.log( this._scoreText.x);
        this._scoreText.y = 80;
        this._scoreText.color = "#FFFF00";

        this._stage.addChild(this._scoreText);
        setInterval(() => this.loop(), 500);
        this._stage.addChild(new UILayer)
    }
    private handleComplete() {
        setTimeout(
            () => this.intervalGo()
            , 100);
    }
    private tickHandler(e) {

        this.loop();
    }
    private loop() {
        if (GameStar.moveY > 1) {
            GameStar.moveY = GameStar.moveY - GameStar.moveY * 0.1;
        } else {
            if (GameStar.moveY != 0)
                GameStar.moveY = 1;
        }

    }

}

// new App();

